<script>
import config from "./config";
import store from "@/store";
import { getToken,setToken } from "@/utils/auth";

export default {
  onBackPress(event) {
    // 处理页面未找到的情况
    console.log("页面未找到:", event);
    uni.redirectTo({
      url: "/", // 重定向到主页或其他页面
    });
  },
  onLaunch: function () {
    // 检查地址栏是否包含被拦截的API路径
    this.checkBlockedUrl();
    
    let token = ''
    token = location.href.indexOf("token") != -1 ? location.href.split("token=")[1].split("#/")[0] : "";
    console.log("看看这个token",token)
    if (token) {
      console.log("这里设置token")
      setToken(token);
      this.$store.commit("SET_TOKEN", token);
      this.$tab.reLaunch("/pages/index");

      this.getUserInfo();
      this.menuList();
      return
    }

    this.initApp();
    uni.addInterceptor("reLaunch", {
      invoke(e) {
        // 因为刷新后进行后退会调用这个路径，所以需要劫持然后调用back方法。
        if (e.url == "/") {
          history.back();
          return false;
        }
      },
    });
  },
  onload() {
 
  },
  methods: {
    // 初始化应用
    initApp() {
      // 初始化应用配置
      this.initConfig();
      // 检查用户登录状态
      //#ifdef H5
      // this.checkLogin();
      //#endif
    },
    getUserInfo(){
      this.$request({
        url:'/user/currentUser',
        method: 'GET',
      }).then(( { data }) => {
        console.log("用户信息",data)
        uni.setStorageSync('user',data)
      })
    },
    menuList() {
      this.$request({
        url: "/menu/userMenu",
        method: "GET",
      }).then(({ data }) => {
        console.log(data, "菜单");
        data.h5.map((item) => {
          if (item.menuName == "知识问答" && item.visible == false) {
            uni.setTabBarItem({
              index: 1,
              visible: false,
            });
          }
		  if (item.menuName == "线上培训" && item.visible == false) {
            uni.setTabBarItem({
              index: 1,
              visible: false,
            });
          }
		  if (item.menuName == "文件管理" && item.visible == false) {
            uni.setTabBarItem({
              index: 1,
              visible: false,
            });
          }
        });
      });
    },
    initConfig() {
      this.globalData.config = config;
    },
    checkLogin() {
      if (!getToken()) {
        console.log("判断没登录");
        this.$tab.reLaunch("/pages/login");
      }
    },
    // 检查地址栏是否包含被拦截的API路径
    checkBlockedUrl() {
      //#ifdef H5
      const currentUrl = location.href;
      const blockedApiPaths = ['/api/actuator'];
      
      // 检查当前URL是否包含被拦截的路径
      const isBlocked = blockedApiPaths.some(blockedPath => 
        currentUrl.includes(blockedPath)
      );
      
      if (isBlocked) {
        console.log("检测到被拦截的API路径:", currentUrl);
        // 立即跳转到404页面
        this.$nextTick(() => {
          uni.reLaunch({
            url: '/pages/common/404/index'
          });
        });
        return true;
      }
      //#endif
      return false;
    },
  },
};
</script>

<style lang="scss">
@import "@/static/scss/index.scss";
</style>
